package Controller;
import com.jfinal.aop.Aop;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import pojo.School;
import service.schoolService;
import java.util.List;


@Path(value = "/school")
public class SchoolController extends Controller {

    static schoolService schoolService = Aop.get(schoolService.class);

    public void index(){
        List<School> schools = schoolService.getSchoolList();
        set("schools",schools);
        render("index.html");
    }
    public void  add(){
        render("add.html");
    }

    public void addschool(){
        String name = get("name");
        String type = get("type");
        String create_year = get("create_year");
        if (schoolService.addSchool(name,type,create_year)){
            redirect("/school/index");
        }else{
            set("errmsg","添加失败");
            forwardAction("/msg/errindex");
        }
    }

    public void delete(){
        String id = getPara(0);
        if (schoolService.deleteByid(id)){
            redirect("/school/index");
        }else{
            set("errmsg","删除失败");
            forwardAction("/msg/errindex");
        }
    }

    public void edit(){
        String id = getPara(0);
        School school = schoolService.getSchoolById(id);
        if (school != null){
            set("school",school);
            render("edit.html");
        }else{
            set("errmsg","数据不存在");
            forwardAction("/msg/errindex");
        }
    }

    public void update(){
        String name = get("name");
        String type = get("type");
        String create_year = get("create_year");
        String id = get("id");
        if (schoolService.update(id,name,type,create_year)){
            redirect("/school/index");
        }else{
            set("errmsg","更新失败");
            forwardAction("/msg/errindex");
        }
    }
}
